WebClient হলো Spring WebFlux-এর অংশ এবং এটি Spring 5 থেকে চালু হয়েছে। এটি একটি নন-ব্লকিং, রিয়্যাকটিভ HTTP ক্লায়েন্ট, যা RestTemplate-এর বিকল্প হিসেবে ব্যবহৃত হয়।
WebClient Bean তৈরি করা
Spring Boot-এ WebClient ব্যবহারের জন্য আপনাকে একটি Bean তৈরি করতে হবে। এটি সাধারণত একটি কনফিগারেশন ক্লাসে তৈরি করা হয়।
উদাহরণ: সাধারণ WebClient Bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder.build(); // ডিফল্ট কনফিগারেশন সহ WebClient
}
}
উদাহরণ: নির্দিষ্ট Base URL সহ WebClient Bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder
.baseUrl("https://api.example.com") // নির্দিষ্ট বেস URL
.build();
}
}
WebClient এর কাস্টম কনফিগারেশন
১. টাইমআউট সেট করা
HTTP রিকোয়েস্টের জন্য টাইমআউট কনফিগার করা যেতে পারে।
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import java.time.Duration;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
HttpClient httpClient = HttpClient.create()
.responseTimeout(Duration.ofSeconds(5)); // রেসপন্স টাইমআউট 5 সেকেন্ড
return builder
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
২. কাস্টম HTTP হেডার যোগ করা
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder
.defaultHeader("Authorization", "Bearer <your-token>") // ডিফল্ট হেডার
.defaultHeader("Content-Type", "application/json") // কনটেন্ট টাইপ
.build();
}
}
৩. লগিং সক্ষম করা (ডিবাগিংয়ের জন্য)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.logging.AdvancedByteBufFormat;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
HttpClient httpClient = HttpClient.create()
.wiretap("reactor.netty.client.HttpClient",
reactor.netty.LogLevel.DEBUG,
AdvancedByteBufFormat.TEXTUAL);
return builder
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
WebClient ব্যবহার করা
GET অনুরোধ:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient webClient) {
this.webClient = webClient;
}
public String getExampleData(String endpoint) {
return webClient.get()
.uri(endpoint)
.retrieve()
.bodyToMono(String.class) // JSON রেসপন্সকে String-এ রূপান্তর
.block(); // সিঙ্ক্রোনাস রেসপন্সের জন্য
}
}
POST অনুরোধ:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient webClient) {
this.webClient = webClient;
}
public String postExampleData(String endpoint, Object payload) {
return webClient.post()
.uri(endpoint)
.bodyValue(payload) // রিকোয়েস্ট বডি
.retrieve()
.bodyToMono(String.class)
.block();
}
}
WebClient টাইমআউট এবং রেসপন্স হ্যান্ডলিং
টাইমআউট সেট করা:
import org.springframework.web.reactive.function.client.WebClient;
import reactor.util.retry.Retry;
import java.time.Duration;
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient webClient) {
this.webClient = webClient;
}
public String getDataWithTimeout(String endpoint) {
return webClient.get()
.uri(endpoint)
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(3)) // 3 সেকেন্ড টাইমআউট
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(1))) // 3 বার রিট্রাই করবে
.block();
}
}
উপসংহার
- WebClient কনফিগারেশনে ফ্লেক্সিবিলিটি: WebClient-কে সহজে কাস্টমাইজ করা যায়, যেমন টাইমআউট, বেস URL, এবং ডিফল্ট হেডার সেট করা।
- নন-ব্লকিং প্রকৃতি: এটি রিয়্যাকটিভ প্রোগ্রামিং এবং উচ্চ-পারফরম্যান্স অ্যাপ্লিকেশনের জন্য আদর্শ।
- ডিপ্রিকেটেড RestTemplate-এর বিকল্প: Spring Boot 2+ এর প্রজেক্টে
WebClientব্যবহারের পরামর্শ দেওয়া হয়।
WebClient Bean তৈরি করে এবং কনফিগার করে আপনি HTTP API কল করা সহজেই ম্যানেজ করতে পারবেন।
Content added By
Read more